VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form FrmNguoiDung 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "Nguoi dung"
   ClientHeight    =   8040
   ClientLeft      =   45
   ClientTop       =   375
   ClientWidth     =   12225
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   8040
   ScaleWidth      =   12225
   ShowInTaskbar   =   0   'False
   StartUpPosition =   3  'Windows Default
   Begin VB.Frame Frame 
      Caption         =   "Thong tin chi tiet"
      Height          =   3255
      Left            =   4200
      TabIndex        =   4
      Top             =   360
      Width           =   7815
      Begin VB.ComboBox cbxLoaiNguoiDung 
         Height          =   315
         Left            =   1560
         TabIndex        =   14
         Top             =   2040
         Width           =   3135
      End
      Begin VB.TextBox txtMatKhau 
         Height          =   375
         Left            =   1560
         TabIndex        =   1
         Top             =   1560
         Width           =   4575
      End
      Begin VB.CommandButton btnCapNhap 
         Caption         =   "Cap nhat"
         Height          =   375
         Left            =   6240
         TabIndex        =   11
         Top             =   2640
         Width           =   1215
      End
      Begin VB.CommandButton btnXoa 
         Caption         =   "Xoa"
         Height          =   375
         Left            =   4920
         TabIndex        =   10
         Top             =   2640
         Width           =   1215
      End
      Begin VB.CommandButton btnThem 
         Caption         =   "Them"
         Height          =   375
         Left            =   3600
         TabIndex        =   9
         Top             =   2640
         Width           =   1215
      End
      Begin VB.CommandButton btnNhapMoi 
         Caption         =   "Nhap moi"
         Height          =   375
         Left            =   960
         TabIndex        =   8
         Top             =   2640
         Width           =   1215
      End
      Begin VB.TextBox txtTenDangNhap 
         Height          =   375
         Left            =   1560
         TabIndex        =   0
         Top             =   1080
         Width           =   4575
      End
      Begin VB.TextBox txtMaNguoiDung 
         Height          =   375
         Left            =   1560
         Locked          =   -1  'True
         TabIndex        =   5
         Top             =   600
         Width           =   2055
      End
      Begin VB.Label Label3 
         Caption         =   "Loai nguoi dung"
         Height          =   255
         Left            =   240
         TabIndex        =   13
         Top             =   2160
         Width           =   1215
      End
      Begin VB.Label Label2 
         Caption         =   "Mat khau"
         Height          =   255
         Left            =   240
         TabIndex        =   12
         Top             =   1680
         Width           =   1215
      End
      Begin VB.Label Label1 
         Caption         =   "Ten dang nhap"
         Height          =   255
         Left            =   240
         TabIndex        =   7
         Top             =   1200
         Width           =   1215
      End
      Begin VB.Label lblStation 
         Caption         =   "Ma nguoi dung"
         Height          =   255
         Left            =   240
         TabIndex        =   6
         Top             =   720
         Width           =   1095
      End
   End
   Begin MSComctlLib.ListView lstvData 
      Height          =   7455
      Left            =   120
      TabIndex        =   2
      Top             =   480
      Width           =   3975
      _ExtentX        =   7011
      _ExtentY        =   13150
      View            =   3
      LabelEdit       =   1
      LabelWrap       =   -1  'True
      HideSelection   =   -1  'True
      FullRowSelect   =   -1  'True
      GridLines       =   -1  'True
      _Version        =   393217
      ForeColor       =   -2147483640
      BackColor       =   -2147483643
      Appearance      =   1
      NumItems        =   0
   End
   Begin VB.Label Label 
      Caption         =   "Danh sach nguoi dung"
      Height          =   255
      Index           =   0
      Left            =   240
      TabIndex        =   3
      Top             =   120
      Width           =   4095
   End
End
Attribute VB_Name = "FrmNguoiDung"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private m_ObjADORst  As ADODB.Recordset
Private m_objNguoiDung As clsNguoiDung
Public EnableBtnChon As Boolean

Private Sub ResetFields()
    txtMaNguoiDung.Text = ""
    txtTenDangNhap.Text = ""
    txtMatKhau.Text = ""
End Sub

Private Sub btnCapNhap_Click()
    Dim objNguoiDungDto As clsNguoiDung
    
    Set m_objNguoiDung = New clsNguoiDung
    Set objNguoiDungDto = New clsNguoiDung
    
    objNguoiDungDto.MaNguoiDung = CInt(Trim(txtMaNguoiDung.Text))
    objNguoiDungDto.TenDangNhap = Trim(txtTenDangNhap.Text)
    objNguoiDungDto.MatKhau = Trim(txtMatKhau.Text)
    
    If m_objNguoiDung.Update(objNguoiDungDto) Then
        LoadData
        MsgBox GetReport("CapNhat", "ThanhCong"), vbInformation, "ThongBao"
    Else
        MsgBox GetReport("CapNhat", "ThatBai"), vbCritical, "ThongBao"
    End If
End Sub

Private Sub btnNhapMoi_Click()
    ResetFields
    txtMaNguoiDung.Locked = False
End Sub

Private Sub btnThem_Click()
    Dim objNguoiDungDto As clsNguoiDung
    Dim errText As String
    
    If Not ValidateInput(errText) Then
        MsgBox errText, vbCritical, "ThongBao"
        Exit Sub
    End If
    
    Set m_objNguoiDung = New clsNguoiDung
    Set objNguoiDungDto = New clsNguoiDung
    
    objNguoiDungDto.TenDangNhap = Trim(txtTenDangNhap.Text)
    objNguoiDungDto.MatKhau = Trim(txtMatKhau.Text)
    objNguoiDungDto.MaLoaiNguoiDung = cbxLoaiNguoiDung.ItemData(cbxLoaiNguoiDung.ListIndex)
    
    If m_objNguoiDung.Insert(objNguoiDungDto) Then
        LoadData
        ResetFields
        MsgBox GetReport("Them", "ThanhCong"), vbInformation, "ThongBao"
    Else
        MsgBox GetReport("Them", "ThatBai"), vbCritical, "ThongBao"
    End If
End Sub

Private Sub btnXoa_Click()
    If Trim(txtMaNguoiDung.Text) = "" Then Exit Sub
    
    Set m_objNguoiDung = New clsNguoiDung
     
    If MsgBox(GetReport("Xoa", "XacNhan"), vbInformation + vbOKCancel, "ThongBao") = vbOK Then
        If m_objNguoiDung.Delete(CInt(txtMaNguoiDung.Text)) Then
            LoadData
            ResetFields
            MsgBox GetReport("Xoa", "ThanhCong"), vbInformation, "ThongBao"
        Else
            MsgBox GetReport("Xoa", "ThatBai"), vbCritical, "ThongBao"
        End If
    End If
End Sub

Private Sub Form_Load()
    Init
    LoadData
    LoadLoaiNguoiDung
    Call ClearSelection(lstvData)
End Sub

Public Sub LoadLoaiNguoiDung()
    Dim i As Integer
    Dim vLstViewItem As ListItem
    Dim objLoaiNguoiDUng As clsLoaiNguoiDung
    
    Set objLoaiNguoiDUng = New clsLoaiNguoiDung
    Set m_ObjADORst = objLoaiNguoiDUng.SelectAll
    cbxLoaiNguoiDung.Clear
    i = 0
    Do While Not m_ObjADORst.EOF
        cbxLoaiNguoiDung.AddItem m_ObjADORst!TenLoaiNguoiDung
        cbxLoaiNguoiDung.ItemData(cbxLoaiNguoiDung.NewIndex) = m_ObjADORst!MaLoaiNguoiDung
        cbxLoaiNguoiDung.ListIndex = i
        m_ObjADORst.MoveNext
        i = i + 1
    Loop
End Sub

Public Sub Init()
    txtMatKhau.PasswordChar = "*"
    lstvData.ColumnHeaders.Clear
    lstvData.ColumnHeaders.Add , , "Ma NguoiDung", lstvData.Width / 3
    lstvData.ColumnHeaders.Add , , "Ten dang nhap", lstvData.Width * 2 / 3
End Sub

Private Sub LoadData()
    Dim i As Integer
    Dim vLstViewItem As ListItem
    
    Set m_objNguoiDung = New clsNguoiDung
    Set m_ObjADORst = m_objNguoiDung.SelectAll
    lstvData.ListItems.Clear
    i = 1
    
    Do While Not m_ObjADORst.EOF
        Set vLstViewItem = lstvData.ListItems.Add(i)
        vLstViewItem = m_ObjADORst!MaNguoiDung
        vLstViewItem.SubItems(1) = m_ObjADORst!TenDangNhap
        m_ObjADORst.MoveNext
        i = i + 1
    Loop
End Sub

Private Sub lstvData_ItemClick(ByVal item As MSComctlLib.ListItem)
    Dim i As Integer
    
    Set m_ObjADORst = m_objNguoiDung.SelectByID(item)
    If Not m_ObjADORst Is Nothing Then
        txtMaNguoiDung.Text = m_ObjADORst!MaNguoiDung
        txtTenDangNhap.Text = m_ObjADORst!TenDangNhap
        txtMatKhau.Text = m_ObjADORst!MatKhau
        
        For i = 0 To cbxLoaiNguoiDung.ListCount - 1
            If UCase$(cbxLoaiNguoiDung.ItemData(i)) = UCase$(m_ObjADORst!MaLoaiNguoiDung) Then
                cbxLoaiNguoiDung.ListIndex = i
                Exit For
            End If
        Next
        
        txtMaNguoiDung.Locked = True
    End If
End Sub



Public Function ValidateInput(ByRef errText As String) As Boolean
    ValidateInput = True
    If Trim(txtTenDangNhap.Text) = "" Then
        errText = "Ban chua nhap ten dang nhap"
        ValidateInput = False
        Exit Function
    End If
    If Trim(txtMatKhau.Text) = "" Then
        errText = "Ban chua nhap mat khau"
        ValidateInput = False
        Exit Function
    End If
    If cbxLoaiNguoiDung.ListIndex = -1 Then
        errText = "Ban chua chon loai nguoi dung"
        ValidateInput = False
        Exit Function
    End If
End Function
